from django.shortcuts import render

from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter

from disease.ser import DepartmentModelSerializer, ScreenModelSerializer, CommonDiseasesModelSerializer, DiseaseSerializer, DiseaseArticlesModelSerializer, DiseaseArticleSerializer
from disease.models import DepartmentModel, ScreenModel, CommonDiseasesModel, Disease, DiseaseArticlesModel, DiseaseArticle

# Create your views here.


# 分页配置
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100

# 排序配置
class CustomOrderingFilter(OrderingFilter):
    ordering_params = ('name', 'publish_date')

# 科室表 ListAPIView
class DepartmentModelListView(ListAPIView):
    serializer_class = DepartmentModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name', 'department_number']

    def get_queryset(self):
        return DepartmentModel.objects.all()

# 筛选表 ListAPIView
class ScreenModelListView(ListAPIView):
    serializer_class = ScreenModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]

    def get_queryset(self):
        return ScreenModel.objects.all()

# 常见疾病表 ListAPIView
class CommonDiseasesModelListView(ListAPIView):
    serializer_class = CommonDiseasesModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]

    def get_queryset(self):
        return CommonDiseasesModel.objects.all()

# 常见疾病详情表 ListAPIView
class DiseaseListView(ListAPIView):
    serializer_class = DiseaseSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]

    def get_queryset(self):
        return Disease.objects.all()

# 疾病表 ListAPIView
class DiseaseArticlesModelListView(ListAPIView):
    serializer_class = DiseaseArticlesModelSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]

    def get_queryset(self):
        return DiseaseArticlesModel.objects.all()

# 疾病文章 ListAPIView
class DiseaseArticleListView(ListAPIView):
    serializer_class = DiseaseArticleSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['publish_date', 'title']

    def get_queryset(self):
        return DiseaseArticle.objects.all()